package com.lwbpro.system.util;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * @Description: 时间转换工具类
 * @Author: 周兆宇
 * @Date: 2022-02-22 08:35:13
 */
public class GetDateUtils {

    /**
     * 功能描述: 计算两个时间的时间差 (天时分秒)
     *
     * @param startTime
     * @param endTime
     * @return java.lang.String 返回 天时分秒格式
     * @author 周兆宇
     * @date 2022-02-22 09:36:14
     */
    public static String getDate(LocalDateTime startTime, LocalDateTime endTime) {
        //获取秒数
        long nowSecond = startTime.toEpochSecond(ZoneOffset.ofHours(0));
        long endSecond = endTime.toEpochSecond(ZoneOffset.ofHours(0));
        long absSeconds = Math.abs(nowSecond - endSecond);
        //获取秒数
        long s = absSeconds % 60;
        //获取分钟数
        long m = absSeconds / 60 % 60;
        //获取小时数
        long h = absSeconds / 60 / 60 % 24;
        //获取天数
        long d = absSeconds / 60 / 60 / 24;

        return d + "天" + h + "时" + m + "分" + s + "秒";
    }

    /**
     * 功能描述: 计算两个时间的时间差 (时分秒)
     *
     * @param startTime
     * @param endTime
     * @return java.lang.String 返回 00:00:00 格式的时分秒差值
     * @author 周兆宇
     * @date 2022-02-22 09:28:55
     */
    public static Long getSecond(LocalDateTime startTime, LocalDateTime endTime) {
        //获取秒数
        long nowSecond = startTime.toEpochSecond(ZoneOffset.ofHours(0));
        long endSecond = endTime.toEpochSecond(ZoneOffset.ofHours(0));
        long absSeconds = Math.abs(nowSecond - endSecond);
        //获取秒数
        long s = absSeconds % 60;
        String second = String.valueOf(s);
        //获取分钟数
        long m = absSeconds / 60 % 60;
        String minute = String.valueOf(m);
        //获取小时数
        long h = absSeconds / 60 / 60;
        String hour = String.valueOf(h);
        // 统一格式为 00:00:00
        second = second.length() == 1 ? "0" + second : second;
        minute = minute.length() == 1 ? "0" + minute : minute;
        hour = hour.length() == 1 ? "0" + hour : hour;
        String time = hour + ":" + minute + ":" + second;

        long seconds = 0;
        // 时分秒格式00:00:00
        if (time.length() == 8) {
            int index1 = time.indexOf(":");
            int index2 = time.indexOf(":", index1 + 1);
            // 小时
            seconds = Integer.parseInt(time.substring(0, index1)) * 3600L;
            // 分钟
            seconds += Integer.parseInt(time.substring(index1 + 1, index2)) * 60L;
            // 秒
            seconds += Integer.parseInt(time.substring(index2 + 1));
        }
        // 分秒格式00:00
        if (time.length() == 5) {
            // 秒  后两位肯定是秒
            seconds = Integer.parseInt(time.substring(time.length() - 2));
            // 分钟
            seconds += Integer.parseInt(time.substring(0, 2)) * 60L;
        }
        return seconds;
    }
}